import 'package:f_flutter_enjoy_plus/api/home.dart';
import 'package:f_flutter_enjoy_plus/home/components/home_list.dart';
import 'package:f_flutter_enjoy_plus/home/components/home_nav.dart';
import 'package:f_flutter_enjoy_plus/utils/Toast.dart';
import 'package:flutter/material.dart';

import '../../utils/TokenManager.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  _IndexState createState() => _IndexState();
}

class _IndexState extends State<HomePage> {
// 获取社区公告列表
  List annoucementList = [];
  @override
  void initState() {
    super.initState();
    getAnnounceList();
  }

  // 获取公告列表
  getAnnounceList() async {
    final res = await getAnnounceListAPI();
    PromptAction.showToast('获取公告列表成功');
    setState(() {
      annoucementList = res;
    });
    print(res);
  }

  // 封装的List Widget -> 操作token
  Widget getListToken() {
    return Row(
      children: [
        Expanded(
          child: ElevatedButton(
            onPressed: () async {
              final token = await TokenManager().getToken();
              PromptAction.showToast('获取到的token是: $token');
            },
            child: const Text('获取token'),
          ),
        ),
        const SizedBox(width: 10),
        Expanded(
          child: ElevatedButton(
            onPressed: () {
              TokenManager().deleteToken();
              PromptAction.showToast('删除token成功');
            },
            child: const Text('删除token'),
          ),
        ),
        const SizedBox(width: 10),
        Expanded(
          child: ElevatedButton(
            onPressed: () {
              TokenManager().setToken('123456');
              PromptAction.showToast('存储token成功');
            },
            child: const Text('存储token'),
          ),
        )
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color.fromRGBO(229, 231, 242, 1),
      appBar: AppBar(
        title: const Text("享+社区"),
        centerTitle: true,
        backgroundColor: Colors.transparent, // 透明背景
      ),
      body: ListView(
        padding: const EdgeInsets.all(10),
        children: [
          getListToken(),
          // 导航条
          const HomeNav(),
          // 轮播图
          ClipRRect(
            borderRadius: BorderRadius.circular(10),
            child: Image.asset('assets/images/banner@2x.jpg'),
          ),
          // 社区公告
          HomeList(notifyList: annoucementList)
        ],
      ),
    );
  }
}
